from __future__ import absolute_import

from django.db import models
from tagging.fields import TagField
from django.db.models import permalink
from django.contrib.auth.models import User
from django.utils.html import escape
import markdown
import datetime


class Page(models.Model):
	name = models.CharField(maxlength="20", primary_key=True, editable=False)
	created = models.DateTimeField(auto_now_add=True)
	modified = models.DateTimeField(auto_now=True)
	author = models.ForeignKey(User, editable=False)
	raw_content =  models.TextField(" ")
	content = models.TextField(blank=True, editable=False)
	tags = TagField()
	private = models.BooleanField(default=False)
	
	def save(self):
		self.content = markdown.markdown(escape(self.raw_content),
		                ['wikilink(base_url=/onk/)'])
		super(Page, self).save()
	
	class Admin:
		pass
	
	def __unicode__(self):
		return "%s" % self.name
	
	def get_absolute_url(self):
		return ('onm.onk.views.view_page', [str(self.pk)])
	get_absolute_url = permalink(get_absolute_url)
		
	def is_author(self, user):
	    return user == self.author
